import Vue from 'vue'
import VueRouter from 'vue-router'
//注：1、可取别名 参考vue.config.js文件 2、都以懒加载形式引入
const Home = () => import('views/Home')  //主页
const Login = () => import('views/common/Login')    //登录
const SignUp = () => import('views/common/SignUp')
const NoteMark  = () => import("../views/notes/NoteMark")
const Notes = () => import("../views/notes/Notes")
import NoteDetail from "../views/notes/NoteDetail";
import Personal from "../views/common/Personal";
import DiscussionArea from "../views/notes/DiscussionArea";
import PublishProblem from "../views/notes/PublishProblem";
import DiscussionDetail from "../views/notes/DiscussionDetail";
import PublishShare from "../views/notes/PublishShare";
import ShareArea from "../views/notes/ShareArea";
import ShareDetail from "../views/notes/ShareDetail";
import CollectionShare from "../views/notes/CollectionShare";
import CollectionProblem from "../views/notes/CollectionProblem";
import TeacherCourse from "../views/course/TeacherCourse";
import MyCourse from "../views/course/MyCourse";
import Statistic from "../views/course/Statistic";
import Charts from "../views/charts/Charts";
import CourseInfo from "../views/course/CourseInfo";
import PPT from "../views/course/PPT";
import PPTList from "../views/course/PPTList";
import Video from "../views/course/Video";
import Booking from "../views/course/Booking";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path: "/signup",
    component:SignUp
  },
  {
    path: '/home',
    meta: {title: '首页'},
    component: Home,
    redirect: '/discussionArea',
    children: [
      {
        path: '/personal',
        meta: {title: '个人中心'},
        component: Personal,
      },
      {
        path: "/publishProblem",
        name: 'PublishProblem',
        meta: {title: '发布问题'},
        component: PublishProblem
      },
      {
        path: "/discussionArea",
        name: 'DiscussionArea',
        meta: {title: '讨论区'},
        component: DiscussionArea
      },
      {
        path: "/publishKnowledge",
        name: "PublishKnowledge",
        meta: {title: "知识共享"},
        component: PublishShare
      },
      {
        path: "/discussionDetail",
        name: 'DiscussionDetail',
        meta: {
          title: '问题详情',
          keepAlive: false
        },
        component:DiscussionDetail

      },
      {
        path: "/shareDetail",
        name: 'ShareDetail',
        meta:{title: "分享详情"},
        component: ShareDetail
      },
      {
        path: "/collectionShare",
        name: "CollectionShare",
        meta: {title: "收藏共享区"},
        component: CollectionShare
      },
      {
        path: "/collectionProblem",
        name: "CollectionProblem",
        meta: {title: "收藏问题区"},
        component: CollectionProblem
      },
      {
        path: "/teachCourse",
        name: "TeachCourse",
        meta: {title: "我教的课"},
        component: TeacherCourse
      },
      {
        path: "/statistic",
        name: "Statistic",
        meta: {title: "统计分析"},
        component: Statistic
      },
      {
        path: "/ppt",
        name: "PPT",
        meta: {title: "ppt"},
        component: PPT
      },
      {
        path: "/booking",
        name: "Booking",
        meta: {title: "学习资料"},
        component: Booking
      },
      {
        path: "/video",
        name: "Video",
        meta: {title: "video"},
        component: Video
      },
      {
        path: "/pptList",
        name: "PPTList",
        meta: {title: "ppt列表"},
        component: PPTList
      },
      {
        path: "/courseInfo",
        name: "CourseInfo",
        meta: {title: "课程信息"},
        component: CourseInfo
      },
      {
        path: '/charts',
        name: 'Charts',
        meta: {title: '聊天'},
        component: Charts
      },
      {
        path: "/myCourse",
        name: "MyCourse",
        meta: {title: "我学的课"},
        component: MyCourse
      },
      {
        path: "/notes",
        name: 'Notes',
        meta: {
          title: '笔记总览',
          keepAlive: true},
        component: Notes
      },
      {
        path: "/noteMark",
        name: "NoteMark",
        meta: {title: '记笔记'},
        component: NoteMark
      },
      {
        path:"/edit",
        name:"BlogEdit",
        meta: {
          title: '编辑笔记',
          keepAlive: false
        },
        component: NoteMark
      },
      {
        path: "/shareArea",
        name: "ShareArea",
        meta: {title: "分享区"},
        component: ShareArea
      },
      {
        path:"/noteDetail",
        name: "NoteDetail",
        meta: {
          title: '笔记详情',
          keepAlive: false
        },
        component: NoteDetail
      }
]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//处理重复点击同一组件错误
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  //获取token
  const tokenStr = window.localStorage.getItem('token');
  console.log('tokenStr' + tokenStr)
  if (!tokenStr) return next('/login');
  next();
})

export default router
